package cn.cheqi.recovery.background.controller;


import cn.cheqi.recovery.background.dto.DataGridView;
import cn.cheqi.recovery.background.dto.ResultDTO;
import cn.cheqi.recovery.background.entity.Owner;
import cn.cheqi.recovery.background.service.OwnerService;
import cn.cheqi.recovery.background.vo.OwnerVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  未入驻业主前端控制器
 * </p>
 *
 * @author Robin
 * @since 2020-02-23
 */
@RestController
@RequestMapping("/system/owner")
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    /**
     * 展示所有未入驻业主列表
     * @param ownerVO
     * @return
     */
    @RequestMapping("/loadAllOwners")
    public DataGridView loadAllOwners(OwnerVO ownerVO){
        IPage<Owner> page = new Page<>(ownerVO.getPage(),ownerVO.getLimit());
        QueryWrapper<Owner> wrapper = new QueryWrapper<>();
        //获取查询体内的业主ID
        Long ownerId = ownerVO.getOwnerId();
        //获取查询体内的业主姓名
        String ownerName = ownerVO.getOwnerName();
        //获取查询体内的业主联系方式
        String ownerPhone = ownerVO.getOwnerPhone();
        //判断这三个条件是否为空,若不是则加进查询条件
        if (ownerId != null){
            wrapper.like("owner_id",ownerId);
        }
        //模糊查询业主姓名
        if (!StringUtils.isBlank(ownerName)){
            //去掉字符串中的空格
            String nameTrim = ownerName.replace(" ", "");
            wrapper.like(nameTrim != null,"owner_name",nameTrim);
        }
        //模糊查询业主联系方式
        if (!StringUtils.isBlank(ownerPhone)){
            //去掉字符串中的空格
            String phoneTrim = ownerPhone.replace(" ", "");
            wrapper.like(phoneTrim != null,"owner_phone",phoneTrim);
        }
        //通过id列倒序输出sql结果
        wrapper.orderByDesc("owner_id");
        ownerService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 添加未入驻业主
     * @param ownerVO
     * @return
     */
    @PostMapping("/addOwner")
    @Transactional
    public ResultDTO addOwner(OwnerVO ownerVO){
        try {
            ownerService.save(ownerVO);
            return new ResultDTO(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO(-1,"添加失败");
        }
    }

    /**
     * 根据未入驻业主ID删除业主
     * @param ownerVO
     * @return
     */
    @PostMapping("/deleteOwnerById")
    @Transactional
    public ResultDTO deleteOwnerById(OwnerVO ownerVO){
        try {
            ownerService.removeById(ownerVO.getOwnerId());
            return new ResultDTO(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO(-1,"删除失败");
        }
    }

    /**
     * 通过未入驻业主ID编辑信息
     * @param ownerVO
     * @return
     */
    @PostMapping("/editOwnerById")
    @Transactional
    public ResultDTO editOwnerById(OwnerVO ownerVO){
        try {
            ownerService.updateById(ownerVO);
            return new ResultDTO(200,"编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO(-1,"编辑失败");
        }
    }
}

